#include "UART/WIFI.h"
#include "math.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
/**
 ****************************************************************************************************
 * DONE 用户定义区
 ****************************************************************************************************
 */



//蓝牙控制小车状态位
uint8_t WIFI_Control_Status = 0;


/**
 ****************************************************************************************************
 * DONE 用户函数区
 ****************************************************************************************************
 */

/**
 * @name 蓝牙状态显示函数
 * @brief 显示蓝牙控制状态
 * @param status 状态变量 
 * @param buffer 被编辑字符串
 * @param buffer_size 字符串长度
 * @retval WIFI_Status_string 标志位字符串
 */
void WIFI_SHOW(uint8_t status, char* buffer, size_t buffer_size) 
{
    const char* status_string = " "; 

    if(status == 1)  status_string = "      ";
    else if(status == 2)  status_string = "front";
    else if(status == 3)  status_string = "back ";
    else if(status == 4)  status_string = "left ";
    else if(status == 5)  status_string = "right";
    else if(status == 6)  status_string = "stop ";
    else if(status == 9)  status_string = "up   ";
    else if(status == 10)  status_string = "dowm ";
    else if(status == 11)  status_string = "left ";
    else if(status == 12)  status_string = "right";
    else status_string = "      ";

    strncpy(buffer, status_string, buffer_size);
    buffer[buffer_size-1] = '\0';
}


/**
 * @brief 解析接收到串口的数据包
 * @param huart 串口句柄
 * @param packet 数据包
 * @param length 数据包长度
 */
void WIFI_ParsePacket(UART_HandleTypeDef *huart,uint8_t *packet, uint16_t length) 
{
	// 将数据包转换为字符串
    char data[length+1];
    memcpy(data, packet, length);
    data[length] = '\0'; // 确保字符串以 NULL 结尾
    
    switch (data[0])
    {
    case 0x00:WIFI_Control_Status = 1;    USART_printf(huart,"%02X\r\n",0x00);
    break;
    case 0x01:WIFI_Control_Status = 2;    USART_printf(huart,"%02X\r\n",0x01);
    break;
    case 0x02:WIFI_Control_Status = 3;    USART_printf(huart,"%02X\r\n",0x02);
    break;
    case 0x03:WIFI_Control_Status = 4;    USART_printf(huart,"%02X\r\n",0x03);
    break;
    case 0x04:WIFI_Control_Status = 5;    USART_printf(huart,"%02X\r\n",0x04);
    break;
    case 0x05:WIFI_Control_Status = 6;    USART_printf(huart,"%02X\r\n",0x05);
    break;
    case 0x06:WIFI_Control_Status = 7;    USART_printf(huart,"%02X\r\n",0x06);
    break;
    case 0x07:WIFI_Control_Status = 8;    USART_printf(huart,"%02X\r\n",0x07);
    break;
    case 0x08:WIFI_Control_Status = 9;    USART_printf(huart,"%02X\r\n",0x08);
    break;
    case 0x09:WIFI_Control_Status = 10;    USART_printf(huart,"%02X\r\n",0x09);
    break;
    case 0x0A:WIFI_Control_Status = 11;    USART_printf(huart,"%02X\r\n",0x0A);
    break;
    case 0x0B:WIFI_Control_Status = 12;    USART_printf(huart,"%02X\r\n",0x0B);
    break;
    default:USART_printf(huart,"%02X\r\n",0xFF);
    break;
    }
}


/**
 * @name 蓝牙控制函数
 * @brief 通过蓝牙控制小车
 */
void WIFI_Control(void)
{
    if(WIFI_Control_Status == 1) CAR_STOP(800);
    else if(WIFI_Control_Status == 2) CAR_FRONT(800);
    else if(WIFI_Control_Status == 3) CAR_BACK(800);
    else if(WIFI_Control_Status == 4) CAR_LEFT(800);
    else if(WIFI_Control_Status == 5) CAR_RIGHT(800);
    else if(WIFI_Control_Status == 6) CAR_STOP(800);
    else if(WIFI_Control_Status == 7) CAR_TURN_LEFT(800);
    else if(WIFI_Control_Status == 8) CAR_TURN_RIGHT(800);
    else if(WIFI_Control_Status == 9) Servo_Control_Advanced(1,SERVO_horizontal,0);
    else if(WIFI_Control_Status == 10) Servo_Control_Advanced(0,SERVO_horizontal,0);
    else if(WIFI_Control_Status == 11) Servo_Control_Advanced(1,SERVO_vertical,0);
    else if(WIFI_Control_Status == 12) Servo_Control_Advanced(0,SERVO_vertical,0);
    else CAR_STOP(800);
}


